/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.enums.Status;
import br.com.foxline.enums.StatusSale;
import br.com.foxline.model.Enterprise;
import br.com.foxline.model.Lot;
import br.com.foxline.model.Reserve;
import br.com.foxline.model.Users;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
public class LotService extends GenericService<Lot> {

    public LotService() {
        super(Lot.class);
    }
    
    @EJB
    private CorrectorService correctorService;

    public List<Lot> lotsByEnterprise(Enterprise enterprise, Integer block) {
        List<Lot> lista = new ArrayList<Lot>();
        try {
            String sql = "Select l from Lot l JOIN l.enterprise e where ";
            if (enterprise != null) {
                sql += " e.id=:enterprise AND ";
            }
            if (enterprise != null && block != null) {
                sql += " l.block=:block AND ";
            }
            sql += " 1=1 order by l.block, l.num ASC";
            Query query = getEntityManager().createQuery(sql);
            if (enterprise != null && block != null) {
                query.setParameter("block", block.toString());
            }
            if (enterprise != null) {
                query.setParameter("enterprise", enterprise.getId());
            }
            lista = query.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }

    public List<Lot> lotsDisponiveis(Enterprise enterprise, Integer block) {
        return lotsDisponiveis(enterprise, block, null);
    }

    public List<Lot> lotsDisponiveis(Enterprise enterprise, Integer block, Integer idLotExcept) {
        List<Lot> lista = new ArrayList<Lot>();
        try {
            String sql = "Select l from Lot l JOIN l.enterprise e where ";
            if (enterprise != null) {
                sql += " e.id=:enterprise AND ";
            }
            if (enterprise != null && block != null) {
                sql += " l.block=:block AND ";
            }
            if (idLotExcept != null) {
                sql += " (l.status = :status OR l.id=:idLot)  order by l.num ASC";
            }else{
                sql += " l.status = :status order by l.num ASC";
            }
            Query query = getEntityManager().createQuery(sql);
            if (idLotExcept != null) {
                query.setParameter("idLot", idLotExcept);
            }
            query.setParameter("status", Status.DISPONIVEL);
            if (enterprise != null && block != null) {
                query.setParameter("block", block.toString());
            }
            if (enterprise != null) {
                query.setParameter("enterprise", enterprise.getId());
            }
            lista = query.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
    
    
    public boolean canSale(Lot l, Users users){
        String sql = "select l from Lot l where l.id=:id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", l.getId());
        Lot lot = (Lot) query.getSingleResult();
        boolean result = true;
        
        if(lot.getStatus().equals(Status.TRAMITE)  || lot.getStatus().equals(Status.VENDIDO)){
            result = false;
        }
        if(lot.getStatus().equals(Status.RESERVADO)){
            sql = "select r from Reserve r where r.lot.id=:id and r.expired=FALSE";
            query = getEntityManager().createQuery(sql);
            query.setParameter("id", l.getId());
            Reserve reserve = (Reserve) query.getSingleResult();
            if(! reserve.getReserverCorrector().getId().equals(correctorService.findCorrectorByUser(users.getLogin()).getId())){
                result = false;
            }
         }
        
        return result;
    }
    
    public List<Lot> getLotToMap(Enterprise enterprise){
        String sql = "Select l from Lot l JOIN l.enterprise e where e.id=:enterprise and l.pathId is not null";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("enterprise", enterprise.getId());
        return query.getResultList();
    }
    
    public Lot getLotByPath(String path){
        String sql = "select l from Lot l where l.pathId=:path";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("path", path);
        return (Lot) query.getSingleResult();
    }
    
     public Lot getLot(Enterprise enterprise, String block, Integer num){
        String sql = "Select l from Lot l JOIN l.enterprise e where e.id=:enterprise and l.block=:block and l.num=:num";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("enterprise", enterprise.getId());
        query.setParameter("block", block);
        query.setParameter("num", num);
        return (Lot)query.getSingleResult();
    }
    
    
}
